<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>出租车的最大盈利</title>
</head>
<body>
  <script>
    function fn(n, rides) {
      rides = rides.sort((a, b) => a[1] - b[1]);
      let dp = new Array(n + 1).fill(0);
      let index = 0;
      dp[0] = 0;
      for (let i = 1; i < dp.length; i++) {
        dp[i] = dp[i - 1];
        while (index < rides.length && i == rides[index][1]) {
          dp[i] = Math.max(dp[rides[index][0]] + rides[index][1] - rides[index][0] + rides[index][2], dp[i - 1], dp[i]);
          index++;
        }
      }
      return dp[dp.length - 1];
    }
    console.log(fn(5, [[2, 5, 4], [1, 5, 1]]))
  </script>
</body>
</html>